package com.tachoknight.geocaching.tsp;

import java.util.ArrayList;
import java.util.List;

import com.tachoknight.geocaching.Waypoint;

public class NearestNeighbor
{
	private List<Waypoint> waypoints;
	private Waypoint startingPoint;
	
	public NearestNeighbor(Waypoint startingPoint, List<Waypoint> waypoints)
	{
		this.startingPoint = startingPoint;
		this.waypoints = waypoints;
	}

	public List<Waypoint> getSortedWaypoints()
	{
		List<Waypoint> sortedWaypoints = new ArrayList<Waypoint>();

		/* Start by assigning the testWP to our starting point */
		Waypoint testWP = startingPoint;
		while (waypoints.size() > 0)
		{
			Waypoint closestWP = testWP.isClosestTo(waypoints);
			sortedWaypoints.add(closestWP);
			waypoints.remove(closestWP);
			testWP = closestWP;
		}

		return sortedWaypoints;
	}
}
